package com.ruoyi.app.fragment

import android.text.TextUtils
import com.bumptech.glide.Glide
import com.drake.net.Get
import com.drake.net.utils.scopeNetLife
import com.hjq.toast.ToastUtils
import com.ruoyi.app.R
import com.ruoyi.app.activity.AboutActivity
import com.ruoyi.app.activity.AvatarActivity
import com.ruoyi.app.activity.HelpActivity
import com.ruoyi.app.activity.SettingActivity
import com.ruoyi.app.activity.mine.EditInfoActivity
import com.ruoyi.app.activity.mine.InfoActivity
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.databinding.FragmentMineBinding
import com.ruoyi.app.model.entity.MineEntity
import com.ruoyi.app.model.entity.UserInfoEntity
import com.ruoyi.app.utils.FlowBus
import com.ruoyi.code.Frame
import com.ruoyi.code.base.BaseBindingFragment
import com.ruoyi.code.witget.toast

class MineFragment : BaseBindingFragment<FragmentMineBinding>() {

    companion object {
        @JvmStatic
        fun newInstance() = MineFragment()
    }

    override fun initView() {
        binding.ivAvatar.setOnClickListener {
            AvatarActivity.startActivity(this.requireActivity())
        }
        binding.tvInfo.setOnClickListener {
            InfoActivity.startActivity(this.requireActivity())
        }
        binding.llEditInfo.setOnClickListener {
            EditInfoActivity.startActivity(this.requireActivity())
        }
        binding.llHelp.setOnClickListener {
            HelpActivity.startActivity(this.requireActivity())
        }
        binding.llAbout.setOnClickListener {
            AboutActivity.startActivity(this.requireActivity())
        }
        binding.llSetting.setOnClickListener {
            SettingActivity.startActivity(this.requireActivity())
        }
        binding.llJiaoliuqun.setOnClickListener {
            ToastUtils.show("QQ群：133713780")
        }
        binding.llZaixiankefu.setOnClickListener {
            ToastUtils.show("模块建设中~")
        }
        binding.llFankuishequ.setOnClickListener {
            ToastUtils.show("模块建设中~")
        }
        binding.llDianzanwomen.setOnClickListener {
            ToastUtils.show("模块建设中~")
        }
    }

    override fun initData() {
        scopeNetLife {
            val body = Get<MineEntity>(ConfigApi.getInfo).await()
            if (body.code == ConfigApi.SUCESSS) {
                val avatar = body.user?.avatar
                if (!TextUtils.isEmpty(avatar)) {
                    Glide.with(Frame.getContext())
                        .load(ConfigApi.baseUrl + body.user?.avatar)
                        .error(R.mipmap.profile)
                        .into(binding.ivAvatar)
                }
                val nickName = body.user?.nickName
                if (TextUtils.isEmpty(nickName)) {
                    binding.nickName.text = "匿名"
                } else {
                    binding.nickName.text = body.user?.nickName
                }
            }
        }

        FlowBus.withStick<UserInfoEntity>(FlowBus.update_user_info).register(this) {
            val imgUrl = it.avatar
            if (!TextUtils.isEmpty(imgUrl)) {
                Glide.with(Frame.getContext())
                    .load(ConfigApi.baseUrl + imgUrl)
                    .error(R.mipmap.profile)
                    .into(binding.ivAvatar)
            }
            it.nickName?.let {
                binding.nickName.text = it
            }
        }

    }

}
